package com.shujia.day10;

import java.util.Objects;
import java.util.TreeSet;

public class Demo02TreeSetTest {
    public static void main(String[] args) {
        TreeSet<Stu> treeSetStu = new TreeSet<>();
        treeSetStu.add(new Stu("张三",120,130,140));
        treeSetStu.add(new Stu("赵六",120.1,130.1,140.1));  // 认为数据重复了
        treeSetStu.add(new Stu("王五",60,70,80));
        treeSetStu.add(new Stu("李四",110,100,90));
        treeSetStu.add(new Stu("麻七",110,100,90));

        System.out.println(treeSetStu);

    }
}

class Stu implements Comparable<Stu>{
    // 姓名,语文成绩,数学成绩,英语成绩
    String name;
    double yw;
    double sx;
    double yy;

    public Stu(String name, double yw, double sx, double yy) {
        this.name = name;
        this.yw = yw;
        this.sx = sx;
        this.yy = yy;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "name='" + name + '\'' +
                ", yw=" + yw +
                ", sx=" + sx +
                ", yy=" + yy +
                '}';
    }

    public double getTotalScore(){
        return this.yw + this.sx + this.yw;
    }

    /*
        Stu other 表示在二叉树中的根节点
        this 表示要添加的数据
        TODO 当compareTo的结果不返回 0 那么就不会对数据进行去重
     */
    @Override
    public int compareTo(Stu other) {
        // 按照总分从高到低输出到控制台
        if (this.getTotalScore() > other.getTotalScore()) {
            return -1;
        } else if (this.getTotalScore() < other.getTotalScore()) {
            return 1;
        }else {
            return 1;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Stu stu = (Stu) o;
        return Double.compare(stu.yw, yw) == 0 && Double.compare(stu.sx, sx) == 0 && Double.compare(stu.yy, yy) == 0 && Objects.equals(name, stu.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, yw, sx, yy);
    }
}